/*
 *  $Id: PlayerLands.cs 1083 2010-05-24 10:28:38Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;
using System.Collections.Generic;

using AgateLib.Geometry;

namespace Ragima {
    /// <summary>
    /// Содержимое таба, определяющее призванную игроком местность
    /// </summary>
    public class PlayerLandsTabContent: TabContent {
        private Player _player, _owner;
        private List<Region> _regions;

        public PlayerLandsTabContent(Player player, Player owner): base(TabType.Lands, false) {
            _player = player;
            _owner = owner;
            _regions = new List<Region>();
            for (int i = 0; i < Constants.FieldSize; i++)
                _regions.Add(new PlayerLandRegion(Constants.GetRowRect(i, 3), this, i));
        }

        public static PlayerLandsTabContent GetContent(Tabs tabs, Player player, Player owner) {
            PlayerLandsTabContent content = null;
            if (tabs != null) content = tabs.FindContent<PlayerLandsTabContent>(delegate(PlayerLandsTabContent other) {
                return other.Player == player && other.Owner == owner;
            });
            return content != null ? content : new PlayerLandsTabContent(player, owner);
        }

        public Player Player { get { return _player; } }
        public Player Owner { get { return _owner; } }
        public bool IsEnemy { get { return _player != _owner; } }

        public override int Count {
            get { return _owner.Lands.Count; }
        }

        public override IEnumerable<Region> GetRegions() {
            for (int i = 0; i < CountFromPosition; i++)
                yield return _regions[i];
        }

        public Land GetLand(int num) {
            int index = GetIndex(num);
            return (index >= 0 && index < Count) ? _owner.Lands[index] : null;
        }
    }

    /// <summary>
    /// Регион, определяющий местность у игрока
    /// </summary>
    public class PlayerLandRegion: HelpRegion {
        private PlayerLandsTabContent _content;
        private int _num;
        private AffectIconHint _hint;

        public PlayerLandRegion(Rectangle rect, PlayerLandsTabContent content, int num): base(rect) {
            _content = content;
            _num = num;
            _hint = new AffectIconHint();
        }

        public Land Land { get { return _content.GetLand(_num); } }
        public bool Selected {
            get { return _content.Player.SelectedLand == Land; }
            set {
                if (value)
                    _content.Player.SelectLand(Land);
                else
                    _content.Player.ResetSelection();
            }
        }

        public bool IsEnemy { get { return _content.IsEnemy; } }

        public override Hint GetHint() {
            Land land = Land;
            if (land == null) return null;
            land.AffectIcons.Update(land.Summary.Icons, Rect.Location);
            return _hint.GetHint(land.AffectIcons.FindByMouse());
        }

        public override void Draw() {
            Land land = Land;
            if (land == null) return;
            land.Card.DrawSmall(Rect.X, Rect.Y);
            land.AffectIcons.Update(land.Summary.Icons, Rect.Location);
            land.AffectIcons.Draw();
            if (Selected)
                Data.CardSelect2.Draw(Rect.Location);
        }
        public override void DrawTarget() {
            Draw();
            Data.CardSelect1.Draw(Rect.Location);
        }

        public override void OnClick() {
            if (_content.IsEnemy) return;
            Selected = !Selected;
        }

        public override IHelpProvider HelpObject {
            get {
                Land land = Land;
                return land != null ? land.Card : null;
            }
        }
    }
}